<?php
/*
* Author: Jason ZHENG <zmj522008@hotmil.com>
* Date  : 2018/07/06
* Class : View 统计阅读量
*/
namespace app\index\controller\command;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class View extends Controller
{ 

	public function index()
	{   
		echo 'View Controller';
	}

	public function ip() {
    //strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
	    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
	        $ip = getenv('HTTP_CLIENT_IP');
	    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
	        $ip = getenv('HTTP_X_FORWARDED_FOR');
	    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
	        $ip = getenv('REMOTE_ADDR');
	    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
	        $ip = $_SERVER['REMOTE_ADDR'];
	    }
	    $res =  preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';

	    $ipAddress = Db::table('ip_address')->where(['ip' => $res, 'type' => 1])->find();
	    if($ipAddress == null){
	    	Db::table('ip_address')->insert(['ip' => $res, 'type' => 1]);
	    }else{
	    	Db::table('ip_address')->where(['ip' => $res, 'type'=>1])->update(['count' => $ipAddress['count']+1]);
	    }
	    
	    header("location:https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzI3MzA1MDg1OQ==&scene=126#wechat_redirect");
	}

	public function get() {
    
	    $ipAddress = Db::table('ip_address')->where(['type' => 1])->select();
	    $data = [];
	    foreach ($ipAddress as $key => $ip) {
	    	$data[$key]['ip'] = $ip['ip'];
	    	$data[$key]['count'] = $ip['count'];
	    }
	    return $data;
	}
}